package com.iot.ic.tcp.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

public abstract class AbstractSendCustomServer implements SendCustomServer
{
    private static final Logger LOG = LoggerFactory.getLogger(AbstractSendCustomServer.class);
    //用于管理所有的channel
    public static final ChannelGroup ALL_CHANNELS = new DefaultChannelGroup("NADRON-CHANNELS", GlobalEventExecutor.INSTANCE);
    @Autowired
    protected SendCustomNettyConfig sendCustomNettyConfig;
    @Autowired
    protected ChannelInitializer<? extends Channel> sendCustomChannelInitializer;


    @Override
    public void startServer(int port) throws Exception 
    {
    	sendCustomNettyConfig.setPortNumber(port);
    	sendCustomNettyConfig.setSocketAddress(new InetSocketAddress(port));
        startServer();
    }

    @Override
    public void startServer(InetSocketAddress socketAddress) throws Exception
    {
    	sendCustomNettyConfig.setSocketAddress(socketAddress);
        startServer();
    }

    @PreDestroy
    @Override
    public void stopServer() throws Exception 
    {
        LOG.debug("In stopServer method of class: {}", this.getClass()
                .getName());
        ChannelGroupFuture future = ALL_CHANNELS.close();
        try 
        {
            future.await();
        } 
        catch (InterruptedException e) 
        {
            LOG.error(
                    "Execption occurred while waiting for channels to close: {}",
                    e);
        } 
        finally 
        {
            if (null != sendCustomNettyConfig.getBossGroup())
            {
            	sendCustomNettyConfig.getBossGroup().shutdownGracefully();
            }
            if (null != sendCustomNettyConfig.getWorkerGroup())
            {
            	sendCustomNettyConfig.getWorkerGroup().shutdownGracefully();
            }
        }
    }

    @Override
    public ChannelInitializer<? extends Channel> getChannelInitializer()
    {
        return sendCustomChannelInitializer;
    }

    // 获取configuration @link(NettyConfig.class)
    @Override
    public SendCustomNettyConfig getNettyConfig() {
        return sendCustomNettyConfig;
    }

    // 获取bossGroup，在spring中配置
    protected EventLoopGroup getBossGroup(){
        return sendCustomNettyConfig.getBossGroup();
    }

    // 获取workerGroup， 在spring中配置
    protected EventLoopGroup getWorkerGroup(){
        return sendCustomNettyConfig.getWorkerGroup();
    }

    @Override
    public InetSocketAddress getSocketAddress()
    {
        return sendCustomNettyConfig.getSocketAddress();
    }

    @Override
    public String toString() 
    {
        return "CustomServer [socketAddress=" + sendCustomNettyConfig.getSocketAddress()
                + ", portNumber=" + sendCustomNettyConfig.getPortNumber() + "]";
    }

}
